package cn.hacynn.nd.controller;

import cn.hacynn.nd.common.result.Result;
import cn.hacynn.nd.common.utils.RedisUtils;
import cn.hacynn.nd.pojo.dto.CodeVerifyDTO;
import cn.hacynn.nd.pojo.dto.TransferFileDTO;
import cn.hacynn.nd.pojo.entity.Share;
import cn.hacynn.nd.pojo.vo.DownloadListVO;
import cn.hacynn.nd.service.ShareService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Hacynn
 * @Date 2024/5/28 22:33
 * @Version 1.0
 */
@RestController
@RequestMapping("/share")
@Slf4j
public class ShareController {

    @Autowired
    private ShareService shareService;
    @Autowired
    private RedisUtils redisUtils;

    /**
     * 获取分享列表
     * @return
     */
    @GetMapping("/list")
    public Result<List<Share>> list(HttpServletRequest request) {
        List<Share> shares = shareService.list(request);
        return Result.success(shares);
    }

    /**
     * 获取分享链接中的文件信息
     * @param shortLink
     * @return
     */
    @GetMapping("/getShareDetail")
    public Result<Share> getShareDetail(@RequestParam String shortLink) {
        log.info("获取分享链接信息:{}",shortLink);
        Share share = shareService.getShareDetail(shortLink);
        //给用户呈现分享链接
        return Result.success(share);
    }

    /**
     * 检查分享文件是否已被删除
     * @param shareId
     * @return
     */
    @GetMapping("/exist")
    public Result exist(@RequestParam String shareId) {
        log.info("检查分享文件是否已被删除，分享id:{}",shareId);
        shareService.exist(shareId);
        return Result.success();
    }

    /**
     * 设置分享文件提取码
     * @param shareId
     * @return
     */
    @PostMapping("/encode")
    public Result<String> encode(@RequestParam String shareId) {
        log.info("设置分享文件提取码，分享文件id:{}",shareId);
        String code = shareService.encode(shareId);
        return Result.success(code);
    }

    /**
     * 使用url短链设置分享文件提取码
     * @param shortLink
     * @return
     */
    @PostMapping("/encode-shortLink")
    public Result<String> encodeShortLink(@RequestParam String shortLink) {
        log.info("使用url短链设置分享文件提取码，短链:{}",shortLink);
        String shareId = (String) redisUtils.get(shortLink);
        String code = shareService.encode(shareId);
        return Result.success(code);
    }

    /**
     * 校验提取码
     * @param codeVerifyDTO
     * @return
     */
    @PostMapping("/verifyCode")
    public Result<Boolean> verifyCode(@RequestBody CodeVerifyDTO codeVerifyDTO) {
        log.info("校验提取码，参数:{}", codeVerifyDTO);
        Boolean b = shareService.verifyCode(codeVerifyDTO);
        return Result.success(b);
    }

    /**
     * 记录分享文件的下载次数
     * @param shareId
     * @return
     */
    @PostMapping("/downloadCount")
    public Result downloadCount(@RequestParam String shareId) {
        shareService.downloadCount(shareId);
        return Result.success();
    }

    /**
     * 分享的文件夹下载（打包下载文件），不需要登录
     * @param dirPath
     * @return
     */
    @PostMapping("/downloadDir")
    public Result<List<DownloadListVO>> downloadDir(@RequestParam(value = "objectPath") String dirPath,
                                                    @RequestParam(value = "shareId") String shareId) {
        log.info("分享文件夹下载（打包下载文件），要下载文件路径:{}", dirPath);
        List<DownloadListVO> list = shareService.downloadDir(shareId, dirPath);
        return Result.success(list);
    }

    /**
     * 取消并删除分享
     * @param shareId
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@RequestParam String shareId) {
        log.info("删除并删除分享，分享id:{}", shareId);
        shareService.delete(shareId);
        return Result.success();
    }

    /**
     * 生成分享二维码
     * @param shareUrl
     * @return
     */
    @GetMapping("/qr")
    public Result<String> generatorQR(@RequestParam String shareUrl) {
        log.info("生成分享二维码，分享地址:{}", shareUrl);
        String urlQR = shareService.generatorQR(shareUrl);
        return Result.success(urlQR);
    }

    /**
     * 转存文件
     * @param transferFileDTO
     * @return
     */
    @PostMapping("/transfer")
    public Result transfer(@RequestBody TransferFileDTO transferFileDTO, HttpServletRequest request) {
        log.info("转存文件:{}", transferFileDTO);
        shareService.transfer(transferFileDTO, request);
        return Result.success();
    }

}
